﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataListCache = null;
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        var unitGroups = getUnitGroup();
        var units = getUnit();
        var series = getSeries();
        var blands = getBland();
        var unitsInGroup = [];
		var searchArg = [];
        var productionTypes = getProductionType();
        
        window.onresize=calSize;
        $(document).ready(function(){
            initBase();
            setData();
            calSize();
		});
        function initBase(){
			$('#ProductionTypeId').combogrid(getComboGridInfo('产品类型','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],productionTypes));
			$('#SeriesId').combogrid(getComboGridInfo('系列','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],series));	
			$('#BlandId').combogrid(getComboGridInfo('品牌','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],blands));							
			$('#UnitGroupId').combogrid(getComboGridInfo('单位组','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:120,align:'right'},
                        ]],unitGroups));
        }
		function unitGroupChanged(n,o){
			var unitInGroup = [];
			for(var i=0;i<units.length;i++){
				if(units[i].UnitGroupId == n)
					unitInGroup.push(units[i]);
			}
			$('#SalesUnitId').combogrid(getComboGridInfo('销售单位','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],unitInGroup));
			$('#UnitId').combogrid(getComboGridInfo('基本单位','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],unitInGroup));
									
        }
        function pageChanging(pageNumber, pageSize) {
            showMask("查找中");
            $.get('../../api/production/list', { Args: searchArg, Length: pageSize, Pos: (pageNumber-1) * pageSize }, function (data) {
                if (data.Result == 0) {
                    dataListCache = JSON.parse(data.Data);
                    $("#productionList").datagrid("loadData", dataListCache);
                    $("#productionList").datagrid('getPager').pagination({ total: data.Count, pageNumber: pageNumber, onSelectPage: pageChanging });
                } else {
                    $.messager.alert('提示', data.Message, 'error');
                }
                hideMask();
            }, 'json');
        }
        function setData() {
            pageChanging(1,10);
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-32;
            $("#productionList").datagrid('resize',{height:height-76,width:width-15});
            $("#toolbar").panel({width:width-15});
            $("#editToolbar").panel({width:width-15});
            $("#editZone").panel('resize',{height:height-76,width:width-15});
        }
		function selectRecord(rowIndex,rowData){
			dataCache = rowData;
		}
        function deleteRecord(){
			if (dataCache == null||dataCache.AutoId==0) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					showMask();
					$.post("../../api/production/remove",dataCache,function(response){
						if(response.Result==0){
							for(var i=0;i<dataListCache.length;i++){
								if(dataListCache[i].AutoId == dataCache.AutoId){
									dataListCache.splice(i,1);
									break;
								}
							}					
							dataCache = null;
							$('#productionList').datagrid('loadData', dataListCache); 
							$.messager.alert("信息","操作成功","info");
						}else{
							$.messager.alert("错误",response.Message,"error");
						}
					})
					.error(function(){
						$.messager.alert("错误","系统内部错误","error");
					})
					.complete(function() { 
						hideMask();
					});	
				}
			});		
		}
        function initVal(){
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
			$('#State').val(dataCache.State);
            $('#Site').val(dataCache.Site);
            $('#Code').textbox("setValue",dataCache.Code);
            $('#Name').textbox("setValue",dataCache.Name);
			$('#SearchCode').textbox("setValue",dataCache.SearchCode);
            $('#Specification').textbox("setValue",dataCache.Specification);
            $('#BarCode').textbox("setValue",dataCache.BarCode);
            $('#UnitGroupId').combogrid("setValue",dataCache.UnitGroupId);
            $('#UnitId').combogrid("setValue",dataCache.UnitId);
            $('#SalesUnitId').combogrid("setValue",dataCache.SalesUnitId);
            $('#BlandId').combogrid("setValue",dataCache.BlandId);
            $('#SeriesId').combogrid("setValue",dataCache.SeriesId);
            $('#ProductionTypeId').combogrid("setValue",dataCache.ProductionTypeId);
            if(dataCache.Promotion){
                document.getElementById("Promotion").checked = true;
            }else{
                document.getElementById("Promotion").checked = false;
            }
            if(dataCache.Spare){
                document.getElementById("Spare").checked = true;
            }else{
                document.getElementById("Spare").checked = false;
            }
            if(dataCache.Virtual){
                document.getElementById("Virtual").checked = true;
            }else{
                document.getElementById("Virtual").checked = false;
            }
            if(dataCache.Free){
                document.getElementById("Free").checked = true;
            }else{
                document.getElementById("Free").checked = false;
            }
            if(dataCache.Subparts){
                document.getElementById("Subparts").checked = true;
            }else{
                document.getElementById("Subparts").checked = false;
            }
        }
        function createNewRecord(){
            dataCache = new Object();
			dataCache.Code=getAutoCode('BAS_PRODUCTION_SET');
            initVal();
            $('#editContent').window('open');
        }
        function editRecord(rowIndex,rowData){
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
        function submitChange(){
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
			var oldData = JSON.parse(JSON.stringify(dataCache));
            showMask();
            dataCache.Flag = $("#Flag").val();
			dataCache.State = $("#State").val();
            dataCache.Code=$("#Code").textbox("getValue");
            dataCache.BarCode=$("#BarCode").textbox("getValue");
			dataCache.SearchCode=$("#SearchCode").textbox("getValue");
            dataCache.Name=$("#Name").textbox("getValue");
            dataCache.Promotion=document.getElementById("Promotion").checked;
            dataCache.Spare=document.getElementById("Spare").checked;
            dataCache.Virtual=document.getElementById("Virtual").checked;
            dataCache.Free=document.getElementById("Free").checked;
            dataCache.Specification=$("#Specification").textbox("getValue");
            dataCache.UnitId=$("#UnitId").combogrid("getValue");
            dataCache.UnitGroupId=$("#UnitGroupId").combogrid("getValue");
            dataCache.SalesUnitId=$("#SalesUnitId").combogrid("getValue");
            dataCache.BlandId=$("#BlandId").combogrid("getValue");
            dataCache.SeriesId=$("#SeriesId").combogrid("getValue");
            dataCache.ProductionTypeId=$("#ProductionTypeId").combogrid("getValue");
            $.post("../../api/production/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
                    if(dataCache.Flag!=1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1,dataCache);
								console.log('列表第'+i+'项被更新');
                                break;
                            }
                        }
                    }else{
						dataListCache.splice(0,0,dataCache);
						console.log('新项被追加到列表');
					}
                    $('#productionList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
					hideMask();
                    $.messager.alert("错误",response.Message,"error");
					dataCache = oldData;
                }
            })
            .error(function(){
				hideMask();
                $.messager.alert("错误","系统内部错误","error");
				dataCache = oldData;
            })
            .complete(function() { 
                hideMask();
            });
        }
        function boolFormatter(value,row,index){
            if(value){
                return "是";
            }else{
                return "否";
            }
        }
                
        function blandFormatter(value, row, index) {
            return valToName(blands,value);
        }        
        function seriesFormatter(value, row, index) {
            return valToName(series,value);
        }        
        function productionTypeFormatter(value, row, index) {
            return valToName(productionTypes,value);
        }
        function unitGroupFormatter(value, row, index) {
			return valToName(unitGroups,value);
        }
        function unitFormatter(value, row, index) {
			return valToName(units,value);
        }
    </script>
<head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
    <div id="toolbar" class="easyui-panel" style="padding:1px;">
        <a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#search').window('open')},iconCls:'icon-search',plain:true">搜索</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:function(){exportData('../../api/production/export',searchArg)} ,iconCls:'icon-pencil',plain:true">导出</a>

    </div>
    <div style="margin:2px"></div>
    <table id="productionList" class="easyui-datagrid"
        data-options="pagination:true,rownumbers: true,singleSelect:true,method:'get',onClickRow:selectRecord,onDblClickRow:editRecord,striped:true,">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
				<th data-options="field:'State',hidden:true"></th>
                <th data-options="field:'Code',width:120,align:'right'">产品代号</th>
                <th data-options="field:'Name',width:120,align:'right'">产品名称</th>
                <th data-options="field:'Specification',width:150,align:'right'">规格</th>
				<th data-options="field:'SearchCode',width:100,align:'right'">短码</th>
				<th data-options="field:'BarCode',width:150,align:'right'">条码</th>
                <th data-options="field:'UnitGroupId',width:100,align:'right',formatter:unitGroupFormatter">单位组</th>
                <th data-options="field:'UnitId',width:80,align:'right',formatter:unitFormatter">基本单位</th>
                <th data-options="field:'SalesUnitId',width:80,align:'right',formatter:unitFormatter">销售单位</th>
                <th data-options="field:'BlandId',width:120,align:'right',formatter:blandFormatter">品牌</th>
                <th data-options="field:'SeriesId',width:120,align:'right',formatter:seriesFormatter">系列</th>
                <th data-options="field:'ProductionTypeId',width:120,align:'right',formatter:productionTypeFormatter">产品类型</th>
                <th data-options="field:'Promotion',width:80,align:'right' ,formatter:boolFormatter">促销品</th>
                <th data-options="field:'Free',width:80,align:'right' ,formatter:boolFormatter">赠品</th>
                <th data-options="field:'Virtual',width:80,align:'right' ,formatter:boolFormatter">虚拟件</th>
                <th data-options="field:'Subparts',width:80,align:'right' ,formatter:boolFormatter">子件</th>
                <th data-options="field:'Spare',width:80,align:'right' ,formatter:boolFormatter">备件</th>
            </tr>
        </thead>
    </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="noheader:true,maximized:true,modal:true,closed:true,iconCls:'icon-save',border:false" style="padding:5px 5px 0px 5px">
        <div class="easyui-panel" id="editToolbar" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#editContent').window('close');},iconCls:'icon-blank',plain:true">关闭</a>
        </div>
        <div style="margin:2px"></div>
        <div id="editZone" class="easyui-panel" style="padding:4px 4px 0px 4px" >
            <table>
                <tr>
                    <td><input class="easyui-textbox" id="Code"  style="width:300px" data-options="label:'产品代号:',required:true"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Name"  style="width:300px" data-options="label:'产品名称:'"></td>
					<td>&nbsp;</td>
					<td><input class="easyui-textbox" id="Specification"  style="width:300px" data-options="label:'规格:'"></td>
                </tr>
                <tr>
                    <td><input class="easyui-textbox" id="SearchCode"  style="width:300px" data-options="label:'短码:'"></td>
					<td>&nbsp;</td>
					<td><input class="easyui-combogrid" id="UnitGroupId"  style="width:300px" data-options="onChange:unitGroupChanged"></td>
					<td>&nbsp;</td>
					<td><input class="easyui-combogrid" id="UnitId"  style="width:300px" data-options="label:'基本单位'"></td>
                </tr>
                <tr>
                    <td><input class="easyui-combogrid" id="SalesUnitId"  style="width:300px" data-options="label:'销售单位'"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-combogrid" id="BlandId"  style="width:300px" ></td>
					<td>&nbsp;</td>
					<td><input class="easyui-combogrid" id="SeriesId"  style="width:300px" ></td>
                    
                </tr>
                <tr>

                    
                </tr>
                <tr>
					<td><input class="easyui-combogrid" id="ProductionTypeId"  style="width:300px"></td>
					<td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="BarCode"  style="width:300px" data-options="label:'条码:'"></td>
                    <td>&nbsp;</td>
                    <td>赠品<input type="checkbox"  id="Free"/></td>                
                </tr>
                <tr>
                    <td>虚拟件<input type="checkbox"  id="Virtual"/></td>
                    <td>&nbsp;</td>
                    <td>子件<input type="checkbox"  id="Subparts"/></td>       
					<td>&nbsp;</td>		
					<td>备件<input type="checkbox"  id="Spare"/></td> 					
                </tr>
                <tr>       
                    <td>促销品<input type="checkbox"  id="Promotion"/></td>
					<td>&nbsp;</td>	
					<td>&nbsp;</td>	
					<td>&nbsp;</td>	
					<td>&nbsp;</td>	
                </tr>
            </table>
            <div hidden>
                <p id="AutoId"></p>
                <p id="Flag"></p>
                <p id="Site"></p>
				<p id="State"></p>
            </div>
			<div id="search" class="easyui-window" title="搜索框" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:400px;padding:10px;">
                <div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_Name" style="width:90%" data-options="label:'品名:'">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_Code" style="width:90%" data-options="label:'品号:'">
                </div>
				<div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_ShortCode" style="width:90%" data-options="label:'短码:'">
                </div>
				<div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_BarCode" style="width:90%" data-options="label:'条码:'">
                </div>
				<div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_Specification" style="width:90%" data-options="label:'规格:'">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-combobox" id="sch_Bland" style="width:90%" data-options="label:'品牌:',valueField:'AutoId',textField:'Name',data:blands">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-combobox" id="sch_Series" style="width:90%" data-options="label:'系列:',valueField:'AutoId',textField:'Name',data:series">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-combobox" id="sch_Type" style="width:90%" data-options="label:'产品类型:',valueField:'AutoId',textField:'Name',data:productionTypes">
                </div>
                <div style="text-align:center;padding:5px 0">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="exeSearch()" style="width:80px">搜索</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearSearch()" style="width:80px">重置</a>
                </div>
                <script>
                function exeSearch() {
                    searchArg = [];
					var s_name = $('#sch_Name').textbox('getValue');
					var s_code = $('#sch_Code').textbox('getValue');
					var s_searchCode = $('#sch_ShortCode').textbox('getValue');
					var s_barCode = $('#sch_BarCode').textbox('getValue');
					var s_specification = $('#sch_Specification').textbox('getValue');
                    var restain1 = {'Key':'Name','Op':'Lk','Value':'%'+s_name+'%'};
                    var restain2 = {'Key':'Code','Op':'Lk','Value':'%'+s_code+'%'};
					var restain3 = {'Key':'SearchCode','Op':'Lk','Value':'%'+s_searchCode+'%'};
					var restain4 = {'Key':'BarCode','Op':'Lk','Value':'%'+s_barCode+'%'};
					var restain5 = {'Key':'Specification','Op':'Lk','Value':'%'+s_specification+'%'};
                    if(s_name!=''){
						searchArg.push(restain1);
					}
					if(s_code!=''){
						searchArg.push(restain2);
					}
					if(s_specification!=''){
						searchArg.push(restain5);
					}
					if(s_searchCode!=''){
						searchArg.push(restain3);
					}
					if(s_barCode!=''){
						searchArg.push(restain4);
					}
                    var sch_rg = $("#sch_Bland").combobox("getValue");
                    if(sch_rg>0){
                        searchArg.push({'Key':'BlandId','Op':'Eq','Value':sch_rg});
                    }
                    var sch_sp = $("#sch_Series").combobox("getValue");
                    if(sch_sp>0){
                        searchArg.push({'Key':'SeriesId','Op':'Eq','Value':sch_sp});
                    }
                    var sch_st = $("#sch_Type").combobox("getValue");
                    if(sch_st>0){
                        searchArg.push({'Key':'ProductionTypeId','Op':'Eq','Value':sch_st});
                    }
                    $('#search').window('close');
                    setData();
                }
                function clearSearch() {
                    $('#sch_Code').textbox('setValue',"");
                    $('#sch_Name').textbox('setValue',"");
					$('#sch_ShortCode').textbox('setValue',"");
					$('#sch_BarCode').textbox('setValue',"");
					$('#sch_Specification').textbox('setValue',"");
                    
                    $("#sch_Bland").combobox("setValue","");
                    $("#sch_Series").combobox("setValue","");
                    $("#sch_Type").combobox("setValue","");
                }
             </script>
            </div>
        </div>
	</div>
</body>
</html>'